package com.example.demo.controller.admin;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.TeacherMap;
import com.example.demo.mapper.UserMap;
import com.example.demo.po.Teacher;
import com.example.demo.po.dto.TeacherAddParam;
import com.example.demo.po.dto.TeacherParam;
import com.example.demo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    TeacherMap teacherMap ;

    @Autowired
    UserMap userMap;

    @Autowired
    TeacherService teacherService;

    //增删改查
    @GetMapping("/get")
    public Result GetTeacherList(){

       List<Teacher> teacherList =  teacherMap.GetTeacherList();
        return ResultUtil.success(teacherList);
    }
    @PostMapping("/save")
    public Result SaveTeacher(@RequestBody TeacherAddParam teacher){

        Result result =    teacherService.SaveTeacher(teacher);

        return result;
    }

    @GetMapping("/delete/{id}")
    public Result DeleteTeacherById(@PathVariable int id ){
        int userid = teacherMap.GetTeacherUserId(id);

        userMap.DeleteUserById(userid);
        teacherMap.DeleteTeacher(id);
        return ResultUtil.success("删除成功!");
    }

    @PostMapping("/update")
    public Result UpdateTeacher(@RequestBody TeacherParam TeacherParam){

        Result result =    teacherService.UpdateTeacher(TeacherParam);

        return result;
    }

    @GetMapping("/search")
    public Result SearchByName(@RequestParam(required = false,defaultValue = "") String name,@RequestParam(required = false,defaultValue = "-1") int id){

        if(StringUtils.isEmpty(name) && id==-1){
            return ResultUtil.success(teacherMap.GetTeacherList());
        }

        List<Teacher> list =    teacherService.SearchTeacher(name,id);



        return ResultUtil.success(list);
    }

    @GetMapping("/getTeacher")
    public Result getTeacher(@RequestParam("userid") int userid){




        return ResultUtil.success(teacherMap.GetTeacherByUserId(userid));
    }


}
